﻿
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>计划库存</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link href="~/js/layui/css/layui.css" rel="stylesheet" />
    <link href="~/X-admin/css/font.css" rel="stylesheet" />
    @* <script src="~/js/layui/layui.js"></script> *@
    @* <script src="~/X-admin/lib/layui/layui.all.js"></script> *@
    <script src="~/X-admin/lib/layui/layui.js"></script>
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/sysframework.js"></script>
    <script src="~/Scripts/formSelect/multiSelect.js"></script>
    <script src="~/js/framework/commonjs.js"></script>
</head>
<body>
    <ul lay-bar="disabled" class="mWebBanner">
        <li class="layui-nav-item" style="float:left;">
            <button type="button" name="btn_update" onclick="sureChoose()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#xe697;</i>确认选择
            </button>
        </li>
  
        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button type="button" onclick="closeWin()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1007;</i>关闭页面
            </button>
        </li>
    </ul>
    <div class="layui-card" style="margin-top:1%;padding-top:2%">
        @* <form id="qForm" name="qForm"> *@
        <form class="layui-form layui-col-space5" style="margin-left:15px" name="qForm" id="qForm">
            <input type="hidden" id="numType" name="numType">
            @* <input type="hidden" id="proCode" name="proCode"> *@
            <input type="hidden" id="typeCode" name="typeCode">
            <input type="hidden" id="gxNames" name="gxNames">
            <div class="layui-inline layui-show-xs-block">
                <input type="text" name="proName" placeholder="名称" autocomplete="off" class="layui-input" id="proName" onchange="loadData()">
            </div>
            <div class="layui-inline layui-show-xs-block">
                <input type="text" name="spec" placeholder="规格" autocomplete="off" class="layui-input" id="spec" onchange="loadData()">
            </div>
        </form>    

        <form class="layui-form layui-col-space5" style="margin-left:15px;margin-top:5px" name="dForm" id="dForm">
            <div class="layui-inline layui-show-xs-block">
                <div class="layui-input-group" style="float:left;">
                    <div class="layui-input-prefix">
                        占用工序
                    </div>
                    <input type="hidden" name="gxCodes" id="gxCodes">
                    <select multiple="multiple" lay-filter="multiple" id="gxCode" name="gxCode"></select>
                </div>

            </div>
        </form>

        <div class="layui-card-body layui-table-body" id="layui-table-main">
            <table class="layui-hide" id="list" lay-filter="list"></table>
        </div>
    </div>

</body>
</html>
<script type="text/html" id="barDemo">

    <a title="明细数据" href="javascript:;" lay-event="view"><i class="layui-icon">&#xe615;</i></a>

</script>

<script>
    // var webCode = "";
    var proCode = "0";
    var numType = "";
    var tCode = "erpStock";
    var plCode = "";
    var unitCode = "";
    $(function () {
        proCode = getUrlVariable("proCode");
        numType = getUrlVariable("numType");
        plCode = getUrlVariable("plCode");
        unitCode = getUrlVariable("unitCode");
        $("input[ name='numType']").val(numType);
        $("input[ name='typeCode']").val(proCode);
        if (numType == "bar") { 
            tCode = "barStock";
        }
        getGxData("gxCode", "");
        // getTableCloums(tCode);
        loadData();
    })

    layui.use(['multiSelect'], function () {
        var $ = layui.jquery, form = layui.form, multiSelect = layui.multiSelect;
    });
    /**
     * 获取多选的状态
     */
    function GetSelectMore() {
        var vals = [],
            texts = [];
        $('select[multiple] option:selected').each(function () {
            vals.push($(this).val());
            texts.push($(this).text());
        })
        $("input[ name='gxCodes']").val(vals.join(','));
        $("input[ name='gxNames']").val(texts.join(','));

    }

    var currPage = "1";
    var cData = new Array();

    function loadData() {
        layui.use('table', function () {
            var conditon = JSON.stringify(FormToJson("qForm"));
            var table = layui.table;
            table.render({
                elem: '#list'
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'barCode', title: '条码编码', width: 130 }
                    , { field: 'proCode', title: '存货编码', width: 100 }
                    , { field: 'proName', title: '存货名称', width: 130 }
                    , { field: 'spec', title: '规格', width: 110 }
                    , { field: 'number', title: '主数量', width: 100 }
                    , { field: 'proWeight', title: '公斤数', width: 100 }
                    , { field: 'proMeter', title: '米数', width: 100 }
                    , { field: 'houseName', title: '仓库', width: 120 }
                    , { field: 'zyNum', title: '已占用数', width: 120 }
                    , { field: 'kyNum', title: '可用数量', width: 120 }
                    , { field: 'useNum', title: '使用数量', width: 120, edit: 'text', fixed: 'right' }
                ]] , done: function (res, curr, count) {
                    currPage = curr;
                    if (unitCode == "meter") {
                        $("[data-field='proWeight']").css('display', 'none');//隐藏列
                    } else if (unitCode == "kg") {
                        $("[data-field='proMeter']").css('display', 'none');//隐藏列
                    } else {
                        $("[data-field='proMeter']").css('display', 'none');//隐藏列
                        $("[data-field='proWeight']").css('display', 'none');//隐藏列
                    }

                    $.each(res['data'], function (i, j) {
                        if (Number(j['kyNum']) <= 0) {
                            TableRowColor('layui-table-main', i, '#FFFF93');
                        } 
                    });
                }
                , url: "../Plan/GetDataList?dataType=sdt&condition=" + conditon
                , skin: 'row' //表格风格
                , even: true
                , page: true //是否显示分页
                , limits: [10, 20, 30]
                , limit: 10 //每页默认显示的数量
            });
            //监听工具条
            table.on('checkbox(list)', function (obj) {
                var checkStatus = table.checkStatus('list');
                cData = new Array();
                for (var i = 0; i < checkStatus.data.length; i++) {
             
                    let keyCode = "",wareHouse="";
                    if (tCode == "barStock") {
                        keyCode = checkStatus.data[i].barCode
                    } else {
                        keyCode = checkStatus.data[i].proCode;
                        wareHouse = checkStatus.data[i].wareHouse
                    }
                    AddMeterialList(keyCode, checkStatus.data[i].kyNum, checkStatus.data[i].useNum, wareHouse, false, checkStatus.data[i].zyPlCodes);
                }
            });

            //监听行工具事件
            table.on('edit(list)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"           
                var data = obj.data;
                let keyCode = "", wareHouse = "";
                if (tCode == "barStock") {
                    keyCode = data.barCode;
                } else {
                    keyCode = data.proCode;
                    wareHouse =data.wareHouse
                }
                var checkStatus = table.checkStatus('list');
                AddMeterialList(keyCode, data.kyNum, data.useNum,wareHouse,true,"");
               // sureChoose();
            });
        });
    }

    function AddMeterialList(keyCode, kyNum, useNum, wareHouse, isEdit, zyPlCodes) {
        if (Number(kyNum) <= 0) {
            layer.msg("该存货已完全被占用，标识码：" + keyCode + ",占用计划:" + zyPlCodes); return;
        }
        if (Number(kyNum) < Number(useNum)) {
            layer.msg("对不起，占用数量已超过可用数量：" + keyCode); return;
        }
        var isAdd = true;
        for (var x = 0; x < cData.length; x++) {
            if (cData[x].keyCode == keyCode) {
                cData[x].number = useNum;
                isAdd = false;
                break;
            }
        }
 
        if (isAdd && !isEdit ) {
            var obj = new Object();
            obj.keyCode = keyCode;
            obj.number = useNum;
            obj.wareHouse = wareHouse;
            cData.push(obj);
        }
    }
    /**
     * 确认选择
     * **/
    function sureChoose() {
        if (cData.length < 1) {
            layer.msg("您没有选择任何数据"); return;
        }

        GetSelectMore();
        var gxCodes = $("#gxCodes").val();
        var gxNames = $("#gxNames").val();

        if (gxCodes==""){
            layer.msg("请选择工序"); return;
        }

        $.ajax({
            url: "../Plan/SavePlanMeterial",
            data: { plCode: plCode, numType: numType, cDatas: JSON.stringify(cData), gxCode: gxCodes, gxName: gxNames },
            type: "post",
            success: function (data) {
                if (data.result) {
                    layer.alert("保存成功", { icon: 6 });
                    loadData();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }

</script>

